// pages/details/details.js
const db = wx.cloud.database(); //初始化数据库
import {
  getTimeNow
} from '../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search: '',
    typeList1: [{
      name: '全部',
    },
    {
      name: '--未分类--',
    },
    {
      name: '二手书',
    },   
    {
      name: '娱乐用品',
    },
    {
      name: '运动器材',
    },
    {
      name: '数码产品',
    },                               
    {
      name: '学习用具',
    },  
    {
      name: '服装鞋帽',
    },  
    {
      name: '生活百货',
    },  
    {
      name: '其他',
    },       
  ],
    a:'',
   /*  tabNow: 0, */
    typeNow1: 0,
    tabList: ['全部', '最热', '最新'], 
    thins: [],
    showMore: false,
    showText: '最新', 
    placeholder: '请输入关键词进行搜索',
    name: '',
  },

/*   selectTab(e) {
    const {
      id
    } = e.currentTarget.dataset;
    this.setData({
      tabNow: id,
    })
    if (id === 0) {
      this.onLoad();
    } else if (id === 1) {
      this.gethot();
    } else if (id === 2) {
      this.getnew();
    }
  }, */

  // 分类筛选
  selectType1(res) {
    const {
      id,
    } = res.currentTarget.dataset;
    this.setData({
      typeNow1: id,
      name: this.data.typeList1[id].name,
    })
    if( this.data.typeNow1 === 0 ) {
      this.onLoad()
      return;
    }

    console.log(this.data.name)
    // 云函数 模糊查询
    wx.cloud.callFunction({
      name: "gettype",
      data: {
        name: this.data.name,
      },
      success: (res) => {
         console.log('6662：',res)
         this.setData({
           thins: res.result.data,
           b: 1
         })
         if(res.result.data.length === 0 || res.result === null) {
          this.setData({
            b: 0
          })
        }
      }
    })


/*     if (id === 0) {
      console.log(res.currentTarget.dataset.name)
      this.onLoad();
    } else if (id === 1) {
      this.gethot();
    } else if (id === 2) {
      this.getnew();
    } */
    


  },

  // 跳转到详情 页面
  to(res) {
    const index = res.currentTarget.dataset.index
    const _id = this.data.thins[index]._id
    const imgdata = this.data.thins[index]
 
    wx.setStorageSync('ershouID', _id)
    wx.cloud.callFunction({
      name: "look",
      data: {
        _id,
        lookNumber: this.data.thins[index].lookNumber,
      },
      success: (res) => {
        console.log("浏览",res)
        //this.onLoad()
      },
      fail: (res) => {
        showToast({
          icon: 'none',
          title: '服务器出错，请重试...'
        })
      }
    })

    const dataobj = {
      lookNumber: imgdata.lookNumber,
      beizhu: imgdata.beizhu,
      createTime: imgdata.createTime,
      imgs: imgdata.imgs,
      phonenumber: imgdata.phonenumber,
      newold: imgdata.newold,
      sell: imgdata.sell,
      state: imgdata.state,
      time: imgdata.time,
      title: imgdata.title,
      two: imgdata.two,
      userInfo: imgdata.userInfo,
      type: imgdata.type,
      _id: imgdata._id,
      _openid: imgdata._openid,
    }
    //跳转
    wx.navigateTo({
      url: '/pages/detailsS/detailsS',
      success: (res) => {
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          data: dataobj
        })
      }
    })


  },

  // 筛选
  showMore() {
    this.setData({
      showMore: !this.data.showMore
    })
  },
  tosearch() {
    wx.navigateTo({
      url: '/pages/search/search' ,
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
 
    wx.showLoading({
      title: '加载中...',
    })
    const that = this
       // 过审核
 
       db.collection("a").get()
         .then(res => {
             console.log("11111111",res.data[0].a)
           that.setData({
             a: res.data[0].a,
           })
         })
    db.collection("ershou").orderBy('createTime','desc').get()
      .then(res => {
        //console.log("全部数据为：", res)
        that.setData({
          thins: res.data,
          b: 1,
        })
      })
    setTimeout(() => {
      wx.hideLoading()
    }, 800)

  },

  // 最热 
  gethot() {
    wx.showLoading({
      title: '加载中...',
    })
    const that = this
    db.collection("ershou").orderBy("lookNumber", "desc").get()
      .then(res => {
        console.log("全部数据为：", res)
        that.setData({
          thins: res.data,
          b: 1
        })
      })
    setTimeout(() => {
      wx.hideLoading()
    }, 800)

    this.setData({
      showMore: false,
      showText: '最热'
    })

  },

  // 最新
  getnew() {
    wx.showLoading({
      title: '加载中...',
    })
    const that = this
    db.collection("ershou").orderBy("createTime", "desc").get()
      .then(res => {
        console.log("全部数据为：", res)
        that.setData({
          thins: res.data,
          b: 1
        })
      })
    setTimeout(() => {
      wx.hideLoading()
    }, 800)

    this.setData({
      showMore: false,
      showText: '最新'
    })

  },

  //  获取输入框值
  searchtext(e) {
    console.log('搜索关键字',e.detail.value)
    this.setData({
      search: e.detail.value,
    })
  },
   
  // 搜索 模糊查询 
  /* getsearch() {
    let key = this.data.search;
    if( key === '' || key === null) {
      wx.showToast({
        title: '输入不能为空噢',
        icon: "none",
        duration: 1000
      })
      return;
    }
    console.log("查询的关键字为：", key)
    const db = wx.cloud.database();
    const _ = db.command
    db.collection('ershou').where(_.or([{
      title: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },
      {
        type: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },
      {
        beizhu: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },
    ])).get({
      success: res => {
        console.log('查询出来的数据为：',res.data)
        this.setData({
          thins: res.data
        })
      },
      fail: err => {
        console.log(err)
      }
    })
  }, */



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {




  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showToast({
      title: '页面刷新中...',
      icon: "none"
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
/*     wx.showToast({
      title: '没有啦~~',
      icon: "none"
    }) */
   if (this.data.typeNow1 === 0) {
      wx.showLoading({
        title: '加载中...',
      })
      db.collection('ershou').skip(this.data.thins.length).get({
        success: (res) => {
          if (res.data.length) {
            res.data.forEach(item => {
              this.data.thins.push(item);
            })
            this.setData({
              thins: this.data.thins,
            })
            wx.hideLoading();
          } else {
            wx.showToast({
              icon: 'none',
              title: '无更多信息',
            })
          }
          //wx.hideLoading();
        },
        fail: (error) => {
          wx.showToast({
            icon: 'none',
            title: '服务器出错...',
          })
          wx.hideLoading();
        }
      })
    } 
    
/*else if (this.data.typeNow1 === 1) {
    wx.showLoading({
      title: '加载中...',
    })
    db.collection('ershou').orderBy("lookNumber", "desc").skip(this.data.thins.length).get({
      success: (res) => {
        if (res.data.length) {
          res.data.forEach(item => {
            this.data.thins.push(item);
          })
          this.setData({
            thins: this.data.thins,
          })
          wx.hideLoading();
        } else {
          wx.showToast({
            icon: 'none',
            title: '无更多信息',
          })
        }
        //wx.hideLoading();
      },
      fail: (error) => {
        wx.showToast({
          icon: 'none',
          title: '服务器出错...',
        })
        wx.hideLoading();
      }
    })
  } else if (this.data.tabNow === 2) {
    wx.showLoading({
      title: '加载中...',
    })
    db.collection('ershou').orderBy("createTime", "desc").skip(this.data.thins.length).get({
      success: (res) => {
        if (res.data.length) {
          res.data.forEach(item => {
            this.data.thins.push(item);
          })
          this.setData({
            thins: this.data.thins,
          })
          wx.hideLoading();
        } else {
          wx.showToast({
            icon: 'none',
            title: '无更多信息',
          })
        }
        //wx.hideLoading();
      },
      fail: (error) => {
        wx.showToast({
          icon: 'none',
          title: '服务器出错...',
        })
        wx.hideLoading();
      }
    })
} */

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {

    console.log('噢噢噢噢',res)
    return {
      title: '二手市场',
      path: 'pages/details/details',
     
    }
  },

  // 发布 
  send() {
    wx.navigateTo({
      url: '/pages/ershou/ershou',
    })
  },

    /* 返回 */
    return () {
      wx.switchTab({
        url: '/pages/School/School',
      })
    },

})